package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.OrderSerial;
import com.uwlaser.service.OrderSerialService;
import com.uwlaser.service.dto.OrderSerialDto;
import com.uwlaser.service.dto.OrderSerialQueryParam;
import com.uwlaser.service.mapper.OrderSerialMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author caoguilin
* @date 2023-05-22
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = OrderSerialService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
@Slf4j
public class OrderSerialServiceImpl extends CommonServiceImpl<OrderSerialMapper, OrderSerial> implements OrderSerialService {

    // private final RedisUtils redisUtils;
    private final OrderSerialMapper orderSerialMapper;

    @Override
    public PageInfo<OrderSerialDto> queryAll(OrderSerialQueryParam query, Pageable pageable) {
        IPage<OrderSerial> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<OrderSerial> page = orderSerialMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, OrderSerialDto.class);
    }

    @Override
    public List<OrderSerialDto> queryAll(OrderSerialQueryParam query){
        return ConvertUtil.convertList(orderSerialMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), OrderSerialDto.class);
    }

    @Override
    public OrderSerial getById(Integer id) {
        return orderSerialMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public OrderSerialDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), OrderSerialDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(OrderSerialDto resources) {
        OrderSerial entity = ConvertUtil.convert(resources, OrderSerial.class);
        return orderSerialMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(OrderSerialDto resources){
        OrderSerial entity = ConvertUtil.convert(resources, OrderSerial.class);
        if(entity.getStartSerial()!=null&&entity.getStartSerial()!=0){
            entity.setLastSerial(entity.getStartSerial());
            log.info("----跳码序列号["+entity.getStartSerial()+"]---------------------------------");
        }
        int ret = orderSerialMapper.updateById(entity);
        log.info("----跳码后当前序列号["+entity.getFirstSerial()+"]---------------------------------");
        log.info("----跳码后下个序列号["+entity.getLastSerial()+"]---------------------------------");
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return orderSerialMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<OrderSerialDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (OrderSerialDto orderSerial : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("开始序列号", orderSerial.getStartserial());
              map.put("前序列号", orderSerial.getFirstserial());
              map.put("下个序列号", orderSerial.getLastserial());
              map.put("工单号", orderSerial.getOrdercode());
              map.put("产品型号", orderSerial.getProductcode());
              map.put("备注", orderSerial.getRemark());
              map.put("工单状态 0:未下发，1：已停止，2：生产中，3：已完成", orderSerial.getOrderStatus());
              map.put("创建人", orderSerial.getCreateBy());
              map.put("创建时间", orderSerial.getCreateTime());
              map.put("更新人", orderSerial.getUpdateBy());
              map.put("更新时间", orderSerial.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
